/**
 * 
 */
package com.yidu.erp.service.BeyondStaff;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yidu.common.ReadStaffExcel;
import com.yidu.common.Tools;
import com.yidu.erp.dao.BeyondAnnexMapper;
import com.yidu.erp.dao.BeyondDepartmentMapper;
import com.yidu.erp.dao.BeyondStaffMapper;
import com.yidu.erp.model.BeyondStaff;

/**
 * 员工service实现类
 * @author wws
 * @date  2017年10月26日
 */
@Service
public class StaffServiceImpl implements StaffService{
	
	@Resource
	private BeyondStaffMapper beyondStaffMapper;
	@Resource
	private BeyondAnnexMapper beyondAnnexMapper;
	@Resource
	private BeyondDepartmentMapper beyondDepartmentMapper;

	@Override
	public Map<String, Object> showList(Map<String, Object> paramMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", beyondStaffMapper.showList(paramMap));
		map.put("count", beyondStaffMapper.countRows(paramMap));
		map.put("code", 0);
		map.put("msg", 0);
		return map;
	}

	@Override
	public int addOrUpdate(BeyondStaff staff) {
		// TODO Auto-generated method stub
		int rows = 0;
		if(Tools.isEmpty(staff.getStaffId())){
			rows = beyondStaffMapper.insertSelective(staff);
		}else{
			rows = beyondStaffMapper.updateByPrimaryKeySelective(staff);
		}
		return rows;
	}

	@Override
	public int delete(String staffId) {
		// TODO Auto-generated method stub
		return beyondStaffMapper.delete(staffId);
	}

	@Override
	public BeyondStaff isLogin(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return beyondStaffMapper.isLogin(paramMap);
	}

	@Override
	public BeyondStaff findById(String staffId) {
		// TODO Auto-generated method stub
		return beyondStaffMapper.selectByPrimaryKey(staffId);
	}

	@Override
	public String readExcelFile(MultipartFile file) {
		String result ="";  
        //创建处理EXCEL的类  
        ReadStaffExcel readExcel=new ReadStaffExcel();  
        //解析excel，获取上传的事件单  
        List<BeyondStaff> staffList = readExcel.getExcelInfo(file);
        int rows = 0;
        System.out.println("-----------------------华丽丽的分界线-----------------------");
        long tiem1 = new Date().getTime();  
        for(int i=0; i<staffList.size(); i++){
        	BeyondStaff staff = staffList.get(i);
        	staff.setAnnexId(beyondAnnexMapper.getAnnexId(staff.getAnnexName()).getAnnexId());
        	staff.setDepartId(beyondDepartmentMapper.getIdByName(staff.getDepartName()));
        	rows += beyondStaffMapper.insertSelective(staff);
        }
        long tiem2 = new Date().getTime();
        System.out.println("一共添加"+rows+"条数据，耗时"+(tiem2-tiem1)+"sm。");
        System.out.println("-----------------------华丽丽的分界线-----------------------");
        //至此已经将excel中的数据转换到list里面了,接下来就可以操作list,可以进行保存到数据库,或者其他操作,  
        //和你具体业务有关,这里不做具体的示范  
        if(rows>0){  
            result = "1";  
        }else{  
            result = "0";  
        }  
        return result;  
	}

	
	@Override
	public String byStaffIdselectRoleName(String staffId) {
		
		return beyondStaffMapper.byStaffIdselectRoleName(staffId);
	}
	  
}
